﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using HSE.Events;
using Microsoft.Practices.Prism.Modularity;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Regions;
using HSE;
using DIST.LR.ZHJG.Events;
using HSE.Common;
using PetroMentor.Service;
using Microsoft.Practices.Prism.ViewModel;
using PetroMentor.Service.Common;
using System.ComponentModel;



namespace PetroMentor
{
    [System.ComponentModel.DesignTimeVisible(false)]
    public partial class HSEShell : UserControl
    {
        Storyboard showContentRegion;
        Storyboard loginOut;
        Storyboard showNaviSb;
        Storyboard hiddenNaviSb;
        Storyboard moduleInSb;
        Storyboard moduleOutSb;
        private IModuleManager _moduleManager;
        private IRegionManager _regionManager;
        private IUnityContainer _container;

        ToolBarNaviInfo _currentNavi;
        public ShellViewModel viewmodel;
        public HSEShell(IModuleManager moduleManager, IUnityContainer container, IRegionManager regionManager)
        {
            InitializeComponent();

            _moduleManager = moduleManager;
            _container = container;
            _regionManager = regionManager;
            viewmodel = new ShellViewModel();
            this.DataContext = viewmodel;

            showContentRegion = this.Resources["sbshowContentRegion"] as Storyboard;
            loginOut = this.Resources["sbLoginOut"] as Storyboard;
            showContentRegion.Completed += new EventHandler(showContentRegion_Completed);
            moduleInSb = (Storyboard)this.Resources["ModuleIn"];
            moduleOutSb = (Storyboard)this.Resources["ModuleOut"];

            moduleOutSb.Completed += new EventHandler(moduleOutSb_Completed);
            EventAggregatorRepository.EventAggregator.GetEvent<NaviMainMenuClickEvent>().Subscribe(NaviRegionHandler);
            EventAggregatorRepository.EventAggregator
                .GetEvent<NaviClickEvent>().Subscribe(NaviClickHandler);

            EventAggregatorRepository.EventAggregator.GetEvent<HSE.Events.LoginClickEvent>().Subscribe(LoginHandler);
            EventAggregatorRepository.EventAggregator
               .GetEvent<BusyIndicatorEvent>().Subscribe(BusyIndicatorHandler);

            EventAggregatorRepository.EventAggregator.GetEvent<LoginOutClickEvent>().Subscribe(LoginOutHandler);
        }

        public void LoginOutHandler(string loginout)
        {
            loginOut.Begin();
        }

        public void BusyIndicatorHandler(BusyInfo info)
        {
            viewmodel.IsBusy = info.IsBusy;
            viewmodel.BusyContent = info.BusyContent;
        }

        void showContentRegion_Completed(object sender, EventArgs e)
        {
            _moduleManager.LoadModule("BannerModule");
            _moduleManager.LoadModule("NavigationModule");
        }

        public void LoginHandler(T_CFG_USER user)
        {
            showContentRegion.Begin();
        }



        public void NaviClickHandler(NaviClickInfo info)
        {
            if (info.Owner == "Home")
            {
                _currentNavi = info.NaviInfo;
                moduleOutSb.Begin();
            }
        }

        public void NaviRegionHandler(string value)
        {
            if (value == "show")
            {
                showNaviSb = (Storyboard)this.Resources["showNavi"];
                showNaviSb.Begin();
            }
            else
            {
                hiddenNaviSb = (Storyboard)this.Resources["hiddenNavi"];
                hiddenNaviSb.Begin();
            }
        }

        void moduleOutSb_Completed(object sender, EventArgs e)
        {
            moduleInSb.Begin();

            _moduleManager.LoadModule(_currentNavi.ModuleName);
            _regionManager.RequestNavigate("ModuleContainer", new Uri(_currentNavi.ViewName, UriKind.Relative));
        }
    }

    public class ShellViewModel : NotificationObject
    {
        public ShellViewModel()
        {
            //Uri endpoint = new Uri(String.Format("{0}DataOperation.ashx?{1}={2}", SystemVar.ServiceUrl, Consts.OP_TYPE, Consts.E_T_CFG_CONFIG));
            //WebClient client = new WebClient();

            ////在异步资源下载操作完成时发生
            //client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
            //client.DownloadStringAsync(endpoint);

        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                if (e.Result != null)
                {
                    List<T_CFG_CONFIG> p;
                    List<T_CFG_CONFIG> list = Util.JsonToObject<List<T_CFG_CONFIG>>(e.Result) as List<T_CFG_CONFIG>;
                    if (list != null)
                    {
                        //EventAggregatorRepository.EventAggregator.GetEvent<Whyl_Aqxs_Query>().Publish(list);
                        //EventAggregatorRepository.EventAggregator.GetEvent<BusyIndicatorEvent>().Publish(new BusyInfo() { IsBusy = false, BusyContent = "数据获取中..." });
                    }
                }


            }

            catch (Exception ex)
            {
                MessageBox.Show(string.Format("获取系统字典发生异常！{0}", ex.Message));
                EventAggregatorRepository.EventAggregator.GetEvent<BusyIndicatorEvent>().Publish(new BusyInfo() { IsBusy = false, BusyContent = "数据获取中..." });

            }
        }

        private bool _isBusy = false;
        public bool IsBusy
        {
            get { return this._isBusy; }
            set
            {
                this._isBusy = value;
                this.RaisePropertyChanged("IsBusy");
            }
        }

        private string _busyContent = "";
        public string BusyContent
        {
            get { return this._busyContent; }
            set
            {
                this._busyContent = value;
                this.RaisePropertyChanged("BusyContent");
            }
        }
    }
}
